package edu.gyc.exceptionhomework;

import java.util.Random;

//不及格异常
public class NotPassException extends RuntimeException {
    private int score;

    public NotPassException(String message, int score) {
        super(message);
        this.score = score;

    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

//留级异常
class RepeaterStudentException extends RuntimeException {
    private int notPassNum;

    public RepeaterStudentException(String message, int notPassNum) {
        super(message);
        this.notPassNum = notPassNum;
    }

    public int getNotPassNum() {
        return notPassNum;
    }

    public void setNotPassNum(int notPassNum) {
        this.notPassNum = notPassNum;
    }
}

//开除异常
class ExpelException extends RuntimeException {
    private Student student;

    public ExpelException(String message, Student student) {
        super(message);
        this.student = student;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

class Student {
    private int id;
    private String name;
    private Integer[] scores = new Integer[10];
    //学习的状态 2 差 1 一般 0好
    private Integer[] studyStatus = new Integer[10];

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer[] getScores() {
        return scores;
    }

    public void setScores(Integer[] scores) {
        this.scores = scores;
    }

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name='" + name + '\'' + '}';
    }

    public void study() {
        Random random = new Random();
        for (int i = 0; i < studyStatus.length; i++) {
            int n = random.nextInt(3);
            studyStatus[i] = n;
        }
        System.out.println(this.name + "的学习状态如下：");
        for (int i = 0; i < studyStatus.length; i++) {
            System.out.println("第" + i + "课：" + studyStatus[i]);
        }
    }

    public void exam() {
        System.out.println(this.name + "开始考试了...");
        Random random = new Random();
        int min = 0, max = 100;
        for (int i = 0; i < studyStatus.length; i++) {
            if (studyStatus[i] == 0) {
                max = 60;
                min = 0;
                scores[i] = min + (int) (Math.random() * (max - min + 1));
            } else if (studyStatus[i] == 1) {
                max = 80;
                min = 60;
                scores[i] = min + (int) (Math.random() * (max - min + 1));
            } else {
                min = 80;
                max = 100;
                scores[i] = min + (int) (Math.random() * (max - min + 1));
            }
        }
        System.out.println(this.name + "的考试成绩如下：");
        for (int i = 0; i < scores.length; i++) {
            System.out.println("第" + i + "课：" + scores[i]);
        }
    }
}

class School {
    private Student[] students = new Student[10];
    private String name;

    public School(String name) {
        this.name = name;
        for (int i = 0; i < 10; i++) {
            students[i] = new Student(i + 1, "学生" + (i + 1));
        }
    }

    public void run() {
        System.out.println(name + "开始教学...");
        for (int i = 0; i < students.length; i++) {
            students[i].study();

        }
        System.out.println(name + "开始考试...");
        for (int i = 0; i < students.length; i++) {
            students[i].exam();

        }
        System.out.println(name + "考试结束，秋后算账开始：");
        check();


    }

    public void check() {

        System.out.println(name + "年终算账：");
        for (int i = 0; i < students.length; i++) {
            try {
                Integer[] n = students[i].getScores();
                int notPassNum = 0;
                for (int j = 0; j < n.length; j++) {
                    try {
                        if (n[j] < 60) {
                            throw new NotPassException(students[i].getName() + ": 第" + j + "课程不及格", n[j]);
                        }
                    } catch (NotPassException e) {
                        notPassNum++;
                        continue;//收到异常仍然可以运行循环
                    }
                }
                if (notPassNum >= 5) {
                    throw new ExpelException(students[i].getName() + ": 被开除", students[i]);

                } else if (notPassNum >= 3 && notPassNum <= 4) {
                    throw new RepeaterStudentException(students[i].getName() + ": 留级", notPassNum);
                } else {
                    System.out.println("Congratulation," + students[i] + "闯关成功！");
                }
            } catch (RepeaterStudentException e) {
                System.err.println(e);
                continue;
            } catch (ExpelException e) {
                System.err.println(e);
                continue;
            } finally {

            }


        }
    }

    public static void main(String[] args) {
        School school = new School("贵阳大学");
        school.run();
    }

}